package com.skyolder.milkbee.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.skyolder.milkbee.domain.Factor;
import com.skyolder.milkbee.service.FactorService;
import com.skyolder.milkbee.web.rest.util.HeaderUtil;
import com.skyolder.milkbee.web.rest.util.PaginationUtil;

import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.inject.Inject;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing Factor.
 */
@RestController
@RequestMapping("/api")
public class FactorResource {

    private final Logger log = LoggerFactory.getLogger(FactorResource.class);
        
    @Inject
    private FactorService factorService;

    /**
     * POST  /factors : Create a new factor.
     *
     * @param factor the factor to create
     * @return the ResponseEntity with status 201 (Created) and with body the new factor, or with status 400 (Bad Request) if the factor has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/factors")
    @Timed
    public ResponseEntity<Factor> createFactor(@RequestBody Factor factor) throws URISyntaxException {
        log.debug("REST request to save Factor : {}", factor);
        return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert("factor", "forbidden", "forbidden create")).body(null);
       /* if (factor.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert("factor", "idexists", "A new factor cannot already have an ID")).body(null);
        }
        
        Factor result = factorService.save(factor);
        return ResponseEntity.created(new URI("/api/factors/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert("factor", result.getId().toString()))
            .body(result);*/
    }

    /**
     * PUT  /factors : Updates an existing factor.
     *
     * @param factor the factor to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated factor,
     * or with status 400 (Bad Request) if the factor is not valid,
     * or with status 500 (Internal Server Error) if the factor couldnt be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/factors")
    @Timed
    public ResponseEntity<Factor> updateFactor(@RequestBody Factor factor) throws URISyntaxException {
        log.debug("REST request to update Factor : {}", factor);
        if (factor.getId() == null) {
            return createFactor(factor);
        }
        Factor result = factorService.save(factor);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert("factor", factor.getId().toString()))
            .body(result);
    }

    /**
     * GET  /factors : get all the factors.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of factors in body
     * @throws URISyntaxException if there is an error to generate the pagination HTTP headers
     */
    @GetMapping("/factors")
    @Timed
    public ResponseEntity<List<Factor>> getAllFactors(@ApiParam Pageable pageable)
        throws URISyntaxException {
        log.debug("REST request to get a page of Factors");
        Page<Factor> page = factorService.findAll(pageable);
        if(page.getContent()==null||page.getContent().isEmpty()){
        	List<Factor> factors=Factor.initAll();
        	for(Factor f:factors){
        		factorService.save(f);
            }
        	page = factorService.findAll(pageable);
        }
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/factors");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /factors/:id : get the "id" factor.
     *
     * @param id the id of the factor to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the factor, or with status 404 (Not Found)
     */
    @GetMapping("/factors/{id}")
    @Timed
    public ResponseEntity<Factor> getFactor(@PathVariable Long id) {
        log.debug("REST request to get Factor : {}", id);
        Factor factor = factorService.findOne(id);
        return Optional.ofNullable(factor)
            .map(result -> new ResponseEntity<>(
                result,
                HttpStatus.OK))
            .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    /**
     * DELETE  /factors/:id : delete the "id" factor.
     *
     * @param id the id of the factor to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/factors/{id}")
    @Timed
    public ResponseEntity<Void> deleteFactor(@PathVariable Long id) {
        log.debug("REST request to delete Factor : {}", id);
        return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert("factor", "forbidden", "forbidden delete")).body(null);
        
       /* factorService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert("factor", id.toString())).build();
    */
    }

}
